package cn.yeyiu.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AlertDialog;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import cn.yeyiu.util.bean.TimeVideoInfo;
import cn.yeyiu.util.bean.VideoInfo;

public class AndroidUtil {
    private static Handler handler;

    public static void setMainHandler(Handler handler2) {
        handler = handler2;
    }

    private static void sendMessage(int what, Object obj) {
        Message obtainMessage = handler.obtainMessage();
        obtainMessage.what = what;
        obtainMessage.obj = obj;
        handler.sendMessage(obtainMessage);
    }

    // toast
    public static void toast(String msg) {
        sendMessage(1, msg);
    }
    // alert 不好，不要使用

    public static void alert(String msg) {
        sendMessage(2, msg);
    }

    public static void forceAlert(String msg) {
        sendMessage(3, msg);
    }

    // 具体使用
    public static void pleaseWait() {
        alert("加载中，请稍后。。。");
    }

    public static void forceWait() {
        forceAlert("正在加载，请耐心等待。。。");
    }

    public static void forceWait(String msg) {
        forceAlert(String.format("正在加载 %s，请耐心等待。。。", msg));
    }

    public static void toastWait() {
        toast("加载中，请稍后。。。");
    }

    public static void longWaitToast() {
        toast("正在加载，请耐心等待。。。");
    }

    public static void waitForToast(String msg) {
        toast(String.format("正在加载 %s，请耐心等待。。。", msg));
    }

    // 网络异常toast
    public static void netErrorToast() {
        toast("网络请求失败，请检查连接！");
    }

    // 浏览器打开
    public static void openInBrowser(Context context, Uri uri) {
        context.startActivity(Intent.createChooser(new Intent("android.intent.action.VIEW", uri), "选择浏览器或播放器"));
    }

    public static void copy(Context context, String data){
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(ClipData.newPlainText("data", data));
        toast("已复制到剪贴板");
    }

    public static void openInBrowser(Context context, String url) {
        openInBrowser(context, Uri.parse(url));
    }

    // 加入收藏
    public static void saveLikes(Context context, VideoInfo videoInfo) {
        ExternalJSONFile.saveDatas(context, ExternalJSONFile.favour, new TimeVideoInfo(videoInfo));
        new AlertDialog.Builder(context).setTitle("(❤ ω ❤)").setMessage("收藏成功啦！").setPositiveButton("OK", (DialogInterface.OnClickListener) null).create().show();
    }

    // 获取raw文件内容
    public static String getRawString(Context context, int raw) {
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(raw), StandardCharsets.UTF_8));
        String tmp;
        StringBuilder sb = new StringBuilder();
        try {
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            br.close();
            return sb.toString();

        } catch (IOException ignore) {
        }
        return null;
    }

    // 获取asset内容列表
    public static List<String> getAssetList(Context context, String folder) {
        try {
            return Arrays.asList(context.getAssets().list(folder));
        } catch (IOException exception) {
            return null;
        }
    }

    // 获取asset文件内容
    public static String getAssetString(Context context, String fileName) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    context.getAssets().open(fileName), StandardCharsets.UTF_8));
            String tmp;
            StringBuilder sb = new StringBuilder();
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            br.close();
            return sb.toString();

        } catch (IOException ignore) {
            ignore.printStackTrace();
        }
        return null;
    }
}
